package cn.bdqn.t354.controller;

import cn.bdqn.t354.pojo.Customer;
import cn.bdqn.t354.pojo.Linkman;
import cn.bdqn.t354.service.CustomerService;
import cn.bdqn.t354.service.LinkmanService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/linkman")
public class LinkmanController {
    @Resource
    private LinkmanService linkmanService;
    @Resource
    private CustomerService customerService;

    /**
     * 跳转联系人页面
     * @param custNo
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(String custNo,Model model){
        //查询客户联系人信息
        List<Linkman> linkmanList=linkmanService.getLinkmanByCustNo(custNo);
        model.addAttribute("linkmanList",linkmanList);

        //查询客户信息
        Customer customer=customerService.getCustomer(custNo);
        model.addAttribute("customer",customer);

        return "linkman/list";
    }

    /**
     * 跳转新建联系人页面
     * @param custNo
     * @param custName
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(String custNo,String custName,Model model){
        model.addAttribute("custNo",custNo);
        model.addAttribute("custName",custName);

        return "linkman/add";
    }

    /**
     * 保存联系人
     * @param linkman
     * @param model
     * @return
     */
    @RequestMapping("/save")
    public String save(Linkman linkman,Model model){
        try {
            linkmanService.addLinkman(linkman);
            //跳转到列表页面
            return "redirect:/linkman/list?custNo="+linkman.getLkmCustNo();
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到编辑页面
            return "redirect:/linkman/add";
        }
    }

    /**
     * 跳转联系人编辑页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Long id,Model model){
        //根据ID，查询联系人信息
        Linkman linkman=linkmanService.getLinkman(id);
        model.addAttribute("linkman",linkman);

        return "linkman/edit";
    }

    /**
     * 修改联系人
     * @param linkman
     * @param model
     * @return
     */
    @RequestMapping("/update")
    public String update(Linkman linkman,Model model){
        try {
            linkmanService.updateLinkman(linkman);
            //跳转到列表页面
            return "redirect:/linkman/list?custNo="+linkman.getLkmCustNo();
        } catch (Exception e) {
            e.printStackTrace();
            //跳转到编辑页面
            return "redirect:/linkman/edit";
        }
    }

    /**
     * 删除联系人
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(Long id,Model model){
        Map<String, Object> map = new HashMap<>();
        try {
            linkmanService.deleteLinkman(id);
            map.put("delResult", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("delResult", false);
        }
        return map;
    }
}
